package controllers.manage.coach;

import controllers.BaseController;
import enums.GoodsType;
import goods.Goods;
import models.venue.coach.Coach;
import models.venue.coach.CoachVO;
import models.venue.policy.Policy;
import order.OrderItem;
import play.Logger;
import play.mvc.Controller;
import utils.DateUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by hanbin on 17/4/10.
 */
public class CoachBespeak extends BaseController {

    public static void index(long id) {
        Coach coach = Coach.findById(id);
        List<CoachVO> amCoachVOList = new ArrayList<>();
        List<CoachVO> pmCoachVOList = new ArrayList<>();
        // 获取当天  2017-10-01
        String todayString = DateUtil.dateToString(new Date() , "yyyy-MM-dd");
        // 年月日 时分秒  上课时间
        Date amBeginAt = DateUtil.stringToDate(todayString + " " + coach.classTimeAM + ":00" , "yyyy-MM-dd HH:mm:ss");
        Date pmBeginAt = DateUtil.stringToDate(todayString + " " + coach.classTimePM + ":00" , "yyyy-MM-dd HH:mm:ss");

        for(int i =0 ; i < coach.classNumberAM; i++) {
            CoachVO classVo = new CoachVO();

            // 下课时间
            Date endAt = DateUtil.getAfterMinutes(amBeginAt , coach.singleTimeLength);
            classVo.beginAt = amBeginAt;
            classVo.endAt = endAt;
            classVo.type = 0;
            Policy policy = Policy.findByCoachAndBeginTimeAndEndTime(coach , DateUtil.dateToString(classVo.beginAt , "HH:mm") , DateUtil.dateToString(classVo.endAt , "HH:mm"));
            classVo.price = policy == null ? BigDecimal.ZERO : policy.price;
            classVo.goodsId = Goods.findOrCreateTypeTimeGoods(GoodsType.COACH , coach, classVo.beginAt , classVo.endAt , classVo.price).id;
            classVo.bookNumber = OrderItem.countByGoods(classVo.goodsId);
            classVo.orderBy = i+"0";
            amCoachVOList.add(classVo);

            CoachVO overVO = new CoachVO();
            overVO.beginAt = endAt;
            overVO.endAt = DateUtil.getAfterMinutes(endAt , coach.timeInterval);
            overVO.type = 1;
            overVO.price = BigDecimal.ZERO;
            overVO.orderBy = i + "1";
            amCoachVOList.add(overVO);

            amBeginAt = overVO.endAt;
        }


        for(int i =0 ; i < coach.classNumberPM; i++) {
            CoachVO classVo = new CoachVO();

            // 下课时间
            Date endAt = DateUtil.getAfterMinutes(pmBeginAt , coach.singleTimeLength);
            classVo.beginAt = pmBeginAt;
            classVo.endAt = endAt;
            classVo.type = 0;
            Policy policy = Policy.findByCoachAndBeginTimeAndEndTime(coach , DateUtil.dateToString(classVo.beginAt , "HH:mm") , DateUtil.dateToString(classVo.endAt , "HH:mm"));
//            Logger.info("Policy : %s" , policy);
            classVo.price = policy == null ? BigDecimal.ZERO : policy.price;
            classVo.goodsId = Goods.findOrCreateTypeTimeGoods(GoodsType.COACH , coach , classVo.beginAt , classVo.endAt , classVo.price).id;
            classVo.bookNumber = OrderItem.countByGoods(classVo.goodsId);
            classVo.orderBy = i+"0";
            pmCoachVOList.add(classVo);

            CoachVO overVO = new CoachVO();
            overVO.beginAt = endAt;
            overVO.endAt = DateUtil.getAfterMinutes(endAt , coach.timeInterval);
            overVO.type = 1;
            overVO.price = BigDecimal.ZERO;
            overVO.orderBy = i + "1";
            pmCoachVOList.add(overVO);

            pmBeginAt = overVO.endAt;
        }



        render(coach , amCoachVOList , pmCoachVOList);
    }
}
